package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.UserMapper;
import com.jieshuibao.mapper.WithdrawExMapper;
import com.jieshuibao.mapper.WithdrawMapper;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.db.Withdraw;
import com.jieshuibao.model.db.WithdrawExample;
import com.jieshuibao.model.db.WithdrawExample.Criteria;
import com.jieshuibao.model.domain.PagePhone;
import com.jieshuibao.wx.util.PayCommonUtil;

@Service
public class WithdrawService{
	
	@Resource
	private WithdrawMapper withdrawMapper;
	@Resource
	private UserMapper userMapper;
	@Resource
	private WithdrawExMapper withdrawExMapper;
	
	public void addWithdraw(Withdraw withdraw) {
		withdrawMapper.insertSelective(withdraw);
	}
	
	public void delWithdrawByUuid(String uuid) {
		withdrawMapper.deleteByPrimaryKey(uuid);
	}
	
	public void updateWithdraw(Withdraw withdraw) {
		withdrawMapper.updateByPrimaryKeySelective(withdraw);
	}
	
	public Withdraw getWithdrawByUuid(String uuid) {
		return withdrawMapper.selectByPrimaryKey(uuid);
	}
	
	public List<Withdraw> getPageWithdraw(WithdrawExample withdrawExample) {
		return withdrawMapper.selectPage(withdrawExample);
	}
	
	public int countWithdraw(WithdrawExample withdrawExample) {
		return withdrawMapper.countByExample(withdrawExample);
	}
	
	//余额提现
	public void  addWithdrawAndUpdateUser(User user,Withdraw withdraw){
		userMapper.updateByPrimaryKeySelective(user);
		withdrawMapper.insertSelective(withdraw);
		
		
	}
	//提现记录
	public List<Withdraw> getWithdrawsByPhone(PagePhone pagePhone){
		return withdrawExMapper.selectWithdrawByPhone(pagePhone);
	}
	/**
	 * 某用户最后一次提现记录
	 * @param phone
	 * @return
	 */
	public List<Withdraw> getLastRecord(String phone){
		WithdrawExample example = new WithdrawExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneNumEqualTo(phone);
		criteria.andStatusEqualTo(PayCommonUtil.PAYSUCCESS_STATUS);
		example.setOrderByClause("createDate DESC");
		example.setPageSize(1);
		return withdrawMapper.selectPage(example );
	}
}
